package com.ctgu.tengyun.util;

import com.ctgu.tengyun.exception.*;
import org.springframework.util.StringUtils;

public class Validator {
	
	public static final Integer COMMENT_MAX_LENGTH = 200;
	
	public static void onResourceNotFound(String msg, String reason, ErrorCodeInterface errorCodeInterface) {
		if (errorCodeInterface == null){
			throw new ResourceNotFoundException(msg, reason);
		}
		throw new ErrorCodeException(errorCodeInterface, msg);
	}
	
	public static void onRequestValidateFailed(String msg, String reason, ErrorCodeInterface errorCodeInterface) {
		if (errorCodeInterface == null){
			throw new RequestValidateException(msg, reason);
		}
		throw new ErrorCodeException(errorCodeInterface, msg);
	}
	
	public static void onForbidden(String msg, String reason, ErrorCodeInterface errorCodeInterface) {
		if (errorCodeInterface == null){
			throw new ForbiddenException(msg, reason);
		}
		throw new ErrorCodeException(errorCodeInterface, msg);
	}
	
	public static void assertHasFields(boolean status, String message) {
		if (status){
			return;
		}
		throw new RequestValidateException(message, message);
	}
	
	
	/**
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	@Deprecated
	public static void assertHasFieldLength(Integer maxLength, String field, String message) {
		assertFieldLength(1, maxLength, field, message);
	}
	
	/**
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	public static void assertHasFieldLength(Integer maxLength, String field, String message, String errorMessage) {
		assertFieldLength(1, maxLength, field, message, errorMessage);
	}
	
	/**
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	@Deprecated
	public static void assertFieldLength(Integer maxLength, String field, String message) {
		assertFieldLength(0, maxLength, field, message);
	}
	
	/**
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	public static void assertFieldLength(Integer maxLength, String field, String message, String errorMessage) {
		assertFieldLength(0, maxLength, field, message, errorMessage);
	}
	
	/**
	 * @param minLength
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	@Deprecated
	public static void assertFieldLength(Integer minLength, Integer maxLength, String field, String message) {
		assertFieldLength(minLength, maxLength, field, message, message);
	}
	
	/**
	 * @param minLength
	 * @param maxLength
	 * @param field 被校验参数
	 * @param message 错误提示
	 * @description 校验参数的长度
	 */
	public static void assertFieldLength(Integer minLength, Integer maxLength, String field, String message, String errorMessage) {
		int length = (StringUtils.isEmpty(field)) ? 0 : field.length();
		if(minLength != null && length < minLength){
			throw new RequestValidateException(message + ", too short", errorMessage);
		}
		if(maxLength != null && length > maxLength){
			throw new RequestValidateException(message + ", too long", errorMessage);
		}
	}
	
	@Deprecated
	public static void assertFieldValue(Long minValue, Long maxValue, Long field, String message) {
		assertFieldValue(minValue, maxValue, field, message, message);
	}
	
	public static void assertFieldValue(Long minValue, Long maxValue, Long field, String message, String errorMessage ) {
		if(minValue != null && minValue > field){
			throw new RequestValidateException(message + ", too small", errorMessage);
		}
		if(maxValue != null && maxValue < field){
			throw new RequestValidateException(message + ", too big", errorMessage);
		}
	}
	
	public static void assertResourceFound(boolean status, String message) {
		assertResourceFound(status, message, message);
	}
	
	public static void assertResourceFound(boolean status, String message, String errorMessage) {
		if (status){
			return;
		}
		
		throw new ResourceNotFoundException(message, errorMessage);
	}
	
	public static void assertNotForbidden(boolean status, String message) {
		if (status){
			return;
		}	
		throw new ForbiddenException(message, message);
	}
	
	@Deprecated
	public static void assertNotForUnAuthorized(boolean status, String message) {
		assertNotForUnAuthorized(status, message, message);
	}
	
	public static void assertNotForUnAuthorized(boolean status, String message, String errorMessage) {
		if (status){
			return;
		}
		
		throw new UnAuthorizedException(message, errorMessage);
	}
	
	public static void assertUnsupported(String message){
		throw new RequestValidateException(message, message);
	}

	public static void assertRequestValidate(boolean status, String message, String errorMessage){
		if (status){
			return;
		}
		
		throw new RequestValidateException(message, errorMessage);
	}
	

	public static void assertTrue(boolean status, String message){
		assertTrue(status, message, message);
	}
	
	
	public static void assertTrue(boolean status, String message, String errorMessage){
		if (status){
			return;
		}		
		throw new RequestValidateException(message, errorMessage);
	}
	
	public static void assertFalse(boolean status, String message){
		assertFalse(status, message, message);
	}
	
	public static void assertFalse(boolean status, String message, String errorMessage){
		if (!status){
			return;
		}
		
		throw new RequestValidateException(message, errorMessage);
	}

	/**open range (minCount, maxCount)*/
	public static final <T extends Comparable<T>> void validateBetweenEx(T count, T minCount, T maxCount) {
		if (count == null) {
			throw new RequestValidateException("no count", "no count");
		} else if (CompareUtil.le(count, minCount)) {
			throw new RequestValidateException("count below threshold. count:" + count + ", minCount:" + minCount, "count below threshold");
		} else if (CompareUtil.ge(count, maxCount)) {
			throw new RequestValidateException("count over threshold. count:" + count + ", maxCount:" + maxCount, "count over threshold");
		}
	}

	/**closed range [minCount, maxCount]*/
	public static final <T extends Comparable<T>> void validateBetweenIn(T count, T minCount, T maxCount) {
		if (count == null) {
			throw new RequestValidateException("no count", "no count");
		} else if (CompareUtil.lt(count, minCount)) {
			throw new RequestValidateException("count below threshold. count:" + count + ", minCount:" + minCount, "count below threshold");
		} else if (CompareUtil.gt(count, maxCount)) {
			throw new RequestValidateException("count over threshold. count:" + count + ", maxCount:" + maxCount, "count over threshold");
		}
	}

	public static final <T extends Comparable<T>> void validateGe(T value, T minIncluded) {
		if (value == null) {
			throw new RequestValidateException("no value", "no value");
		}

		if (!CompareUtil.ge(value, minIncluded)) {
			throw new RequestValidateException("not ge:" + minIncluded, "over threshold");
		}
	}

	public static final <T extends Comparable<T>> void validateGt(T value, T minExcluded) {
		if (value == null) {
			throw new RequestValidateException("no value", "no value");
		}

		if (!CompareUtil.gt(value, minExcluded)) {
			throw new RequestValidateException("not gt:" + minExcluded, "over threshold");
		}

	}

	public static final <T extends Comparable<T>> void validateLe(T value, T maxIncluded) {
		if (value == null) {
			throw new RequestValidateException("no value", "over threshold");
		}

		if (!CompareUtil.le(value, maxIncluded)) {
			throw new RequestValidateException("not le:" + maxIncluded, "over threshold");
		}
	}

	public static final <T extends Comparable<T>> void validateLt(T value, T maxExcluded) {
		if (value == null) {
			throw new RequestValidateException("no value", "over threshold");
		}

		if (!CompareUtil.lt(value, maxExcluded)) {
			throw new RequestValidateException("not le:" + maxExcluded, "over threshold");
		}
	}
}
